$(function(){
	var prefix = "/schlimit";
     
	$('#btn_save').click(function(){
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/update.do",
            data: $('#exampleForm').serialize(),
            error : function(request) {
                toastr.error("Connection error");
            },
            success : function(data) {
                if (data.code == 0) {
                    parent.toastr.success("操作成功");
                    parent.reload();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                } else {
                    toastr.warning(data.msg)
                }
            }
        });
    })
     //禁用所有输入框
function readonlyInput() {
    $("#exampleForm").find("input,textarea,checkbox,radio").attr("readonly", true);
    $("#exampleForm").find("select").prop("disabled", true);
    $("#btn_save").hide();//隐藏操作按钮
}

//初始化验证规则
function initValidator() {
    $('#exampleForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            id: {
                message: '人员A不能为空',
                validators: {
                    notEmpty: {
                        message: '此项不能为空'
                    },
                    different: {
                        field: 'id',
                        message: '不能同人员B相同'
                    }
                }
            },
            id: {
                message: '人员B不能为空',
                validators: {
                    notEmpty: {
                        message: '此项不能为空'
                    },
                    different: {
                        field: 'id',
                        message: '不能同人员A相同'
                    }
                }
            }
        }
    });
}
})

/*var prefix = "/schlimit";
    var isReadonly = "[[${isReadonly}]]";//页面是否只能查看
    $(function () {
        //初始化下列框
        //select2InitialForCNamePCode("#id",'人员姓名/拼音',null,[[${schlimit.id}]]);  //不分人员类型
        //select2InitialForCNamePCode("#id",'人员姓名/拼音',null,[[${schlimit.id}]]);
        select2CommonAsync("#id","/select2/getStaffPCode.do","人员姓名/首字母",[[${schlimit.id}]],null);
        select2CommonAsync("#id","/select2/getStaffPCode.do","人员姓名/首字母",[[${schlimit.id}]],null);

        $('#id').on('change',function(){
            $('#exampleForm').data('bootstrapValidator')
                .updateStatus('id', 'NOT_VALIDATED', null)
                .validateField('id');
        }); // Notify only Select2 of changes
        $('#id').on('change',function(){
            $('#exampleForm').data('bootstrapValidator')
                .updateStatus('id', 'NOT_VALIDATED', null)
                .validateField('id');
        }); // Notify only Select2 of changes
        if (isReadonly === "1") {
            readonlyInput();
        } else {
            initValidator();
        }

        if([[${schlimit.id}]] != null){
            $("#id").val([[${schlimit.id}]]);
        }
    });

    //禁用所有输入框
    function readonlyInput() {
        $("#exampleForm").find("input,textarea,checkbox,radio").attr("readonly", true);
        $("#exampleForm").find("select").prop("disabled", true);
        $("#btn_save").hide();//隐藏操作按钮
    }

    //保存
    function save() {
    	alert(111);
        $("#exampleForm").data('bootstrapValidator').validate();//触发一次所有的验证
        if (!$("#exampleForm").data('bootstrapValidator').isValid()) {//判断验证是否已经通过
            return;
        }

        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/update.do",
            data: $('#exampleForm').serialize(),
            error : function(request) {
                toastr.error("Connection error");
            },
            success : function(data) {
                if (data.code == 0) {
                    parent.toastr.success("操作成功");
                    parent.reload();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                } else {
                    toastr.warning(data.msg)
                }
            }
        });
    }

    //初始化验证规则
    function initValidator() {
        $('#exampleForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                id: {
                    message: '人员A不能为空',
                    validators: {
                        notEmpty: {
                            message: '此项不能为空'
                        },
                        different: {
                            field: 'id',
                            message: '不能同人员B相同'
                        }
                    }
                },
                id: {
                    message: '人员B不能为空',
                    validators: {
                        notEmpty: {
                            message: '此项不能为空'
                        },
                        different: {
                            field: 'id',
                            message: '不能同人员A相同'
                        }
                    }
                }
            }
        });
    }*/